package com.leo.base;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 * Created by leo on 2016/11/21.
 */
public class AnnotationDispose {
    private  static Logger logger = LoggerFactory.getLogger(AnnotationDispose.class);
    public static void run() throws Exception {
        ClassUtils.traversalClass("com.leo", new ClassUtils.IClassFoundCallBack() {
            @Override
            public void procClassName(String className) {
                try{
                Field[] fields=Class.forName(className).getDeclaredFields();
                InputStream inStream = ClassUtils.class.getClassLoader().getResourceAsStream("app.properties");
                Properties props = new Properties();
                props.load(inStream);
                for(Field f:fields)
                {
                    if(f.getAnnotation(Config.class)!=null)
                    {
                        Object o=Class.forName(className).newInstance();
                        f.setAccessible(true);
                        f.set(o, props.getProperty(f.getAnnotation(Config.class).name()));
                        logger.info("Load property ["+className+"]:"+f.getAnnotation(Config.class).name()+"="+f.get(o));
                    }
                }}catch ( Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}
